package com.bestcem.xm.common.core.sql;

import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.common.core.constant.RegTypes;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Optional;

/**
 * @author ming.xue <ming.xue@idiaoyan.com>
 * @desc 动态条件参数代理
 * @date 2021-05-25
 */
public class ParamsProxy {
    /**
     * 被代理的参数
     */
    private final JSONObject params;

    public ParamsProxy(JSONObject params) {
        if (params == null) {
            throw new ConditionGrammarException("params missing");
        }

        this.params = params;
    }

    /**
     * 获取参数值
     *
     * @param fieldName
     * @return
     */
    public Optional<String> getStringValue(String fieldName) {
        String value = params.getString(fieldName);
        return Optional.ofNullable(value);
    }

    /**
     * 初始化扩展字段参数
     *
     * @param conditionProxy
     */
    public void initExtendFieldParam(ConditionProxy conditionProxy) {
        String fieldName = (String) conditionProxy.getRawValue(ConditionConstant.NAME);
        if (StringUtils.isBlank(fieldName)) {
            throw new ConditionGrammarException("condition.name missing");
        }

        if (fieldName.startsWith(ConditionConstant.FIELD_VAL_LIST)) {
            String[] fieldNameParts = fieldName.split(RegTypes.DOT);
            if (fieldNameParts.length < 2) {
                throw new ConditionGrammarException("condition.name malformed");
            }
            String queryFieldName = fieldNameParts[0];
            String queryFieldValueName = fieldNameParts[fieldNameParts.length - 1];
            @SuppressWarnings("unchecked")
            List<JSONObject> fieldValues = (List<JSONObject>) params.get(queryFieldName);
            if (fieldValues == null || fieldValues.isEmpty()) {
                return;
            }

            // 获取条件中的扩展字段标识
            JSONObject detail = conditionProxy.getDetail();
            String conditionFieldId = detail == null ? null : detail.getString(ConditionConstant.FIELD_ID);
            if (StringUtils.isBlank(conditionFieldId)) {
                // 动态条件不完整
                throw new ConditionGrammarException("condition.detail.field_id missing");
            }

            for (JSONObject fieldVal : fieldValues) {
                if (fieldVal == null) {
                    continue;
                }

                String fieldId = fieldVal.getString(ConditionConstant.FIELD_ID);
                if (StringUtils.equals(fieldId, conditionFieldId)) {
                    params.put(fieldName, fieldVal.get(queryFieldValueName));
                    break;
                }
            }
        }
    }

    /**
     * 获取地址
     *
     * @param addressPartIndex
     * @param addressKey
     * @return
     */
    public String getAddress(int addressPartIndex, String addressKey) {
        List<String> addressParts = (List<String>) this.params.get(addressKey);
        if (CollectionUtils.isEmpty(addressParts) || addressPartIndex < 0 || addressParts.size() <= addressPartIndex) {
            return StringUtils.EMPTY;
        }
        return addressParts.get(addressPartIndex);
    }
}
